package com.zglt;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.lang.math.NumberUtils;

import com.dalin.common.clazz.Clazz;
import com.dalin.common.clazz.Property;
import com.dalin.common.clazz.PropertyList;
import com.dalin.common.utils.StringUtils;
import com.dalin.common.utils.TextUtils;
import com.dalin.platform.autocode.AutocodeConst;

public class CodeMain {
	private static String pool = "langfangdev2";
	private static String loginName = "test_tang22";
	private static String userName = "lizj322";
	private static String userId = "39249136742a41799d4d5e9725594586";
	private static String availableZoneId = "26aed212-b739-4687-8e23-1356292ae331";
	private static String baseUrl = "http://172.20.2.131:8081/wocloud-vpcserver/vpc/queryRouter?pool=langfangdev2&loginName=jianghui522&userName=jianghui522";
	private void aa() {
		//iMonitor.monitorRouterInfo(arg0)
	}
	
	public static void main(String[] args) {
		Double d = Double.parseDouble("53660876800")/1024/1024/1024;
		System.out.println(d);
	}

	public static void test(Object... objs) {
		System.out.println(objs[0]+"=="+objs[0].getClass().equals(String.class)+"=="+objs[0].getClass().getName());
		System.out.println(objs[1]+"=="+objs[1].getClass().equals(Integer.class)+"=="+objs[1].getClass().getName());
	}

	public static void buildMethod(String methodName) throws Exception {
		List<Property> propList = buildReqClazz(methodName).getAll();
		StringBuffer psb = new StringBuffer();
		// njlSetPropteyList
		StringBuffer vosetsb = new StringBuffer();

		List<String> l = TextUtils.readByList(new File(
				"C:/ews/cutedalin/src/com/zglt/templateMethod.txt"), "UTF-8");
		for (int i = 0; i < propList.size(); i++) {
			Property p = propList.get(i);
			String pName = p.getName();
			String pType = p.getType();
			if (i > 0) {
				psb.append(",\n           		");
				vosetsb.append("\n        ");
			}
			psb.append("@ApiParam(\"参数" + pName + "\") @RequestParam(value=\""
					+ pName + "\", required=false) " + pType + " " + pName);
			vosetsb.append("vo.set" + StringUtils.toUpperCamelCase(pName) + "("
					+ pName + ");");
		}
		StringBuffer sb = new StringBuffer();
		for (String s : l) {
			s = s.replaceAll("njlMethodName", methodName)
					.replaceAll("njlRequests", psb.toString())
					.replaceAll("njlBigMethodName",
							StringUtils.toUpperCamelCase(methodName))
					.replaceAll("njlSetPropteyList", vosetsb.toString());
			sb.append(s + "\n");
		}
		TextUtils.overwriteToText(sb.toString(), new File(
				"C:/ews/cutedalin/src/com/zglt/code/" + "methodName.java"));
	}

	public static PropertyList buildReqClazz(String njlMethodName)
			throws Exception {
		PropertyList allParamList = new PropertyList();
		Clazz myClazz = new Clazz();
		myClazz.setPackageName("cn.wocloud.agent.restapi.consumer");
		myClazz.setClassName(StringUtils.toUpperCamelCase(njlMethodName)
				+ "Req");
		PropertyList propertyList = new PropertyList();
		String s = TextUtils.readNoLine(new File(
				"C:/ews/cutedalin/src/com/zglt/src.txt"), "UTF-8");
		JSONObject obj = JSONObject.fromObject(s);
		Iterator it = obj.keys();
		while (it.hasNext()) {
			String key = (String) it.next();
			String value = obj.getString(key);
			try { // 创建属性对象
				JSONObject sobj = JSONObject.fromObject(value);
				Iterator sit = sobj.keys();
				Clazz smyClazz = new Clazz();
				smyClazz.setPackageName("cn.wocloud.agent.restapi.consumer");
				smyClazz.setClassName(StringUtils.toUpperCamelCase(key));
				PropertyList spropertyList = new PropertyList();
				while (sit.hasNext()) {
					String skey = (String) sit.next();
					String svalue = sobj.getString(skey);
					String type = "String";
					if (NumberUtils.isNumber(svalue))
						type = "int";
					spropertyList.add(type, skey, "请求参数");
					allParamList.add(type, skey, "请求参数");

				}
				smyClazz.setPropertyList(spropertyList);
				smyClazz.setMethodList(AutocodeConst
						.getGetSetMethodList(spropertyList));
				TextUtils.overwriteToText(smyClazz.toString(),
						new File("C:/ews/cutedalin/src/com/zglt/code/"
								+ smyClazz.getClassName() + ".java"));
			} catch (JSONException e) {
				// System.out.println(key+"=="+value);
				String type = "String";
				if (NumberUtils.isNumber(value))
					type = "int";
				propertyList.add(type, key, "");
				allParamList.add(type, key, "");
				continue;
			}
			propertyList.add(StringUtils.toUpperCamelCase(key), key, "请求参数");
			// System.out.println(key+"=obj="+value);
		}
		myClazz.setPropertyList(propertyList);
		myClazz.setMethodList(AutocodeConst.getGetSetMethodList(propertyList));
		TextUtils.overwriteToText(myClazz.toString(), new File(
				"C:/ews/cutedalin/src/com/zglt/code/" + myClazz.getClassName()
						+ ".java"));
		return allParamList;
	}

	public static void buildResClazz(String njlMethodName) throws Exception {
		Clazz myClazz = new Clazz();
		myClazz.setPackageName("cn.wocloud.agent.restapi.consumer");
		myClazz.setClassName(StringUtils.toUpperCamelCase(njlMethodName)
				+ "Res");
		TextUtils.overwriteToText(myClazz.toString(), new File(
				"C:/ews/cutedalin/src/com/zglt/code/" + myClazz.getClassName()
						+ ".java"));
	}
	
	 public static String sendGet(String url) {
	        String result = "";
	        BufferedReader in = null;
	        try {
	            String urlNameString = url;
	            URL realUrl = new URL(urlNameString);
	            // 打开和URL之间的连接
	            URLConnection connection = realUrl.openConnection();
	            // 设置通用的请求属性
	            connection.setRequestProperty("Accept", "*/*");
	            connection.setRequestProperty("Connection", "keep-alive");
	            connection.setRequestProperty("User-Agent",
	                    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
	            // 建立实际的连接
	            connection.connect();
	            // 获取所有响应头字段
	            Map<String, List<String>> map = connection.getHeaderFields();
	            // 遍历所有的响应头字段
	            for (String key : map.keySet()) {
	                System.out.println(key + "--->" + map.get(key));
	            }
	            // 定义 BufferedReader输入流来读取URL的响应
	            in = new BufferedReader(new InputStreamReader(
	                    connection.getInputStream()));
	            String line;
	            while ((line = in.readLine()) != null) {
	                result += line;
	            }
	        } catch (Exception e) {
	            System.out.println("发送GET请求出现异常！" + e);
	            e.printStackTrace();
	        }
	        // 使用finally块来关闭输入流
	        finally {
	            try {
	                if (in != null) {
	                    in.close();
	                }
	            } catch (Exception e2) {
	                e2.printStackTrace();
	            }
	        }
	        return result;
	    }

	    /**
	     * 向指定 URL 发送POST方法的请求
	     * 
	     * @param url
	     *            发送请求的 URL
	     * @param param
	     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	     * @return 所代表远程资源的响应结果
	     */
	    public static String sendPost(String url, String param) {
	        PrintWriter out = null;
	        BufferedReader in = null;
	        String result = "";
	        try {
	            URL realUrl = new URL(url);
	            // 打开和URL之间的连接
	            URLConnection conn = realUrl.openConnection();
	            // 设置通用的请求属性
	            conn.setRequestProperty("accept", "*/*");
	            conn.setRequestProperty("connection", "Keep-Alive");
	            conn.setRequestProperty("user-agent",
	                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
	            // 发送POST请求必须设置如下两行
	            conn.setDoOutput(true);
	            conn.setDoInput(true);
	            // 获取URLConnection对象对应的输出流
	            out = new PrintWriter(conn.getOutputStream());
	            // 发送请求参数
	            out.print(param);
	            // flush输出流的缓冲
	            out.flush();
	            // 定义BufferedReader输入流来读取URL的响应
	            in = new BufferedReader(
	                    new InputStreamReader(conn.getInputStream()));
	            String line;
	            while ((line = in.readLine()) != null) {
	                result += line;
	            }
	        } catch (Exception e) {
	            System.out.println("发送 POST 请求出现异常！"+e);
	            e.printStackTrace();
	        }
	        //使用finally块来关闭输出流、输入流
	        finally{
	            try{
	                if(out!=null){
	                    out.close();
	                }
	                if(in!=null){
	                    in.close();
	                }
	            }
	            catch(IOException ex){
	                ex.printStackTrace();
	            }
	        }
	        return result;
	    }    
	}

